<template>
  <t-base-table hover stripe bordered tableLayout="auto" row-key="index" :data="tableData" :columns="columns"
    lazy-load></t-base-table>
</template>

<script lang="ts" setup>
import type { TableProps } from 'tdesign-vue-next'
import { ref, onMounted } from 'vue'
import { useStatusStore } from '@/stores'
import axios from 'axios'

const statusStore = useStatusStore()
const columns = ref<TableProps['columns']>([
  { colKey: 'name', title: '服务器名称' },
  { colKey: 'id', title: '服务器id' },
  { colKey: 'status', title: '状态' },
  { colKey: 'ipaddr', title: 'IP地址' },
  { colKey: 'uptime', title: '上线时间' },
])

const tableData = ref<TableProps['data']>([
  // { id: 1, name: 'test1', status: '良好', ipaddr: '192.168.10.31', uptime: new Date("2025-05-16T10:18:00Z").toLocaleString() },

])

onMounted(() => {
  axios.post('/api/broadcast_command?command=collect_server_status')
    .then(({ data }) => {
      for (let item in data) {
        data[item].message = JSON.parse(data[item].message)
        tableData.value?.push({
          id: item,
          name: data[item].message.node_name,
          status: getWorkLoadDescription(data[item].message['5min_load']),
          ipaddr:data[item].message.ipv4,
          uptime:data[item].message.uptime
        })
      }
      console.log(data)
    }).catch((err) => {
      console.error('[ServerList]Error Getting ServerList: ', err)
    })
})

/**
 * 获得工作负载状态字符串（>0.5警告，0.7危险）
 */
function getWorkLoadDescription(workLoad: number) {
  if (workLoad <= 0.5) return '良好'
  else if (workLoad <= 0.7) return '警告'
  else return '危险'
}

</script>

<style scoped></style>
